package csu.edu.flatprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import csu.edu.flatprogram.common.CommonResponse;
import csu.edu.flatprogram.entity.Account;
import csu.edu.flatprogram.entity.DonateGoods;
import csu.edu.flatprogram.persistence.AccountMapper;
import csu.edu.flatprogram.persistence.DonateGoodsMapper;
import csu.edu.flatprogram.service.AccountService;
import csu.edu.flatprogram.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.sql.Wrapper;
import java.util.List;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private DonateGoodsMapper donateGoodsMapper;

    //查询所有account用户信息
    @Override
    public CommonResponse<List<Account>> getAllAccounts() {
        List<Account> accountList = accountMapper.selectList(null);
        if(accountList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有accounts信息");
        }
        return CommonResponse.createForSuccess(accountList);
    }

    //通过id查询单个account用户信息
    @Override
    public CommonResponse<Account> getAccountByUserid(String userid) {
        Account account = accountMapper.selectById(userid);

        if(account == null){
            return CommonResponse.createForSuccessMessage("没有该id的用户");
        }

        return CommonResponse.createForSuccess(account);
    }


    //根据account用户ID查询用户所捐赠的物品信息
    @Override
    public CommonResponse<List<DonateGoods>> getDonateGoodsByAccountId(String userid) {
        QueryWrapper<DonateGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);

        List<DonateGoods> donateGoodsList = donateGoodsMapper.selectList(queryWrapper);
        if(donateGoodsList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该募捐信息下暂时没有募捐物品");
        }

        return CommonResponse.createForSuccess(donateGoodsList);
    }

    //新增一条用户信息
    @Transactional
    @Override
    public CommonResponse<Account> addAccount(String userid, String password, String username,String sex, String phone, String address) {
       Account account = new Account();
       account.setUserid(userid);
       account.setPassword(password);
       account.setUsername(username);
        account.setSex(sex);
       account.setPhone(phone);
       account.setAddress(address);
       int i = accountMapper.insert(account);
       return CommonResponse.createForSuccess("已新增了"+userid+"的用户信息",account);

    }

    //根据ID更改用户信息
    @Override
    public CommonResponse<Account> updateAccountById(String userid, String password, String username,String sex, String phone, String address) {
        Account account = accountMapper.selectById(userid);
        account.setPassword(password);
        account.setUsername(username);
        account.setSex(sex);
        account.setPhone(phone);
        account.setAddress(address);
        int i = accountMapper.updateById(account);
        return CommonResponse.createForSuccess("已修改了"+userid+"的用户信息",account);
//        }
    }

    //根据Id删除一条用户信息
    @Override
    public CommonResponse<Account> deleteAccountById(String userid) {
        int i = accountMapper.deleteById(userid);
        return CommonResponse.createForSuccessMessage("删除成功");
    }

    //添加用户
    @Override
    public CommonResponse<Account> insertAccount(Account account) {
        int i = accountMapper.selectCount(Wrappers.<Account>query().eq("userid",account.getUserid()));
        if(i>0){
            return CommonResponse.createForError("用户名已存在");
        }

        Account account1 = new Account();
        account1.setUserid(account.getUserid());
        String password = account.getPassword();
        // 将密码进行MD5加密
        password= MD5Utils.md5(password);
        account1.setPassword(password);
        account1.setUsername(account.getUsername());
        account1.setSex(account.getSex());
        account1.setPhone(account.getPhone());
        account1.setAddress(account.getAddress());

        i = accountMapper.insert(account1);
        if(i == 0){
            return CommonResponse.createForError("添加失败");
        }
        return CommonResponse.createForSuccess("添加成功",account1);
//        return null;
    }

    //更新用户信息
    @Override
    public CommonResponse<Account> updateAccount(Account account) {
        int i = accountMapper.selectCount(Wrappers.<Account>query().eq("userid",account.getUserid()));
        if(i == 0){
            return CommonResponse.createForError("用户名不存在");
        }

        Account account1 = new Account();
        account1.setUserid(account.getUserid());
        String password = account.getPassword();
        // 将密码进行MD5加密
        password= MD5Utils.md5(password);
        account1.setPassword(password);
        account1.setUsername(account.getUsername());
        account1.setSex(account.getSex());
        account1.setPhone(account.getPhone());
        account1.setAddress(account.getAddress());

        i = accountMapper.update(account1,Wrappers.<Account>query().eq("userid",account.getUsername()));
//        if(i == 0){
//            return CommonResponse.createForError("更新失败");
//        }
        return CommonResponse.createForSuccess("更新成功",account1);
    }

    //根据Id查询单个募捐物品
    @Override
    public CommonResponse<DonateGoods> getDonateGoodById(int goodsid) {
        DonateGoods donateGoods = donateGoodsMapper.selectById(goodsid);
        if(donateGoods == null){
            return CommonResponse.createForSuccessMessage("没有该ID的剧增物品");
        }

        return CommonResponse.createForSuccess(donateGoods);
    }
}
